﻿Imports System.Web.UI.WebControls
Imports System.Web.UI
Imports DBase.scafolding.Config
Imports DBase.MySql

Namespace DBase.Scafolding.CustomControl

    Public Class SearchView
        Inherits WebControl
        Implements INamingContainer

        Public Event EventOpen()

        Dim WithEvents TxtFieldID As New TextBox
        Dim WithEvents BtnSearch As New Button
        Dim WithEvents Grid As New DBase.Scafolding.CustomControl.TableView
        Dim WithEvents pnlGrid As New Panel

        Public Sub New()
            Me.EnsureChildControls()
            Me.InitControl()
        End Sub

        Private _filtro As Integer
        Public Property filtro() As Integer
            Get
                Return Utils.Conv.ToInt(Me.TxtFieldID.Text)
            End Get
            Set(ByVal value As Integer)
                Me.TxtFieldID.Text = value
            End Set
        End Property

        Private _field As Field
        Public Property field() As Field
            Get
                Return _field
            End Get
            Set(ByVal value As Field)
                _field = value
                SetConfig()
            End Set
        End Property

        Private Sub InitControl()

            TxtFieldID.Enabled = False
            TxtFieldID.Visible = False
            BtnSearch.Text = "Open"
            Grid.CssClass = "table_result_light_search"
            pnlGrid.CssClass = "Panel_Search_Grid"
            Grid.Visible = False

        End Sub

        Private Sub SetConfig()

            Me.Grid.Config = Me.field.ConfigTable
            Me.pnlGrid.Controls.Add(Grid)

        End Sub

        Private Sub CustomControlSearch_Load(sender As Object, e As System.EventArgs) Handles Me.Load
            If Not Me.Page.IsPostBack Then
                Me.Grid.Visible = False
            End If
        End Sub

        Protected Overrides Sub CreateChildControls()
            Controls.Clear()
            Controls.Add(TxtFieldID)
            Controls.Add(BtnSearch)
            Controls.Add(New LiteralControl("<br />"))
            Controls.Add(pnlGrid)

        End Sub

        Protected Overrides Sub Render(ByVal output As HtmlTextWriter)
            For Each c As Object In Controls
                c.RenderControl(output)
            Next
        End Sub

        Protected Sub BtnSearch_Click(sender As Object, e As System.EventArgs) Handles BtnSearch.Click
            Me.Grid.Visible = Not Me.Grid.Visible
            Dim ColumnNameFilter As New Dictionary(Of String, String)
            ColumnNameFilter.Add(Me.field.JoinFieldLink, Me.filtro.ToString)
            Me.Grid.FiltroGridValue = ColumnNameFilter
            Me.Grid.Bind()

            RaiseEvent EventOpen()

        End Sub

    End Class
End Namespace
